﻿Imports System.Data.SqlClient

Public Class frmPlanOperacion
    Private Rutas As New DataTable("Nombres")
    Private RutasAutocomplete As New AutoCompleteStringCollection

    Private Sub frmPlanOperacion_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
        Rutas.PrimaryKey = Nothing
        Rutas.Clear()
        Rutas.Columns.Clear()
    End Sub
    Private Sub frmPlanOperacion_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ToolStrip1.Cursor = Cursors.Hand
        ToolStrip1.RenderMode = System.Windows.Forms.ToolStripRenderMode.System

        CargaRutas()
        Dim claves(0) As DataColumn
        claves(0) = Rutas.Columns("Nombre")
        Rutas.PrimaryKey = claves

        For Each Nombre In Rutas.Rows
            RutasAutocomplete.Add(Nombre("Nombre"))
        Next
    End Sub
    Private Sub ControlEdicion(ByVal sender As Object, ByVal e As DataGridViewEditingControlShowingEventArgs) Handles dgPlan.EditingControlShowing
        If dgPlan.CurrentCell.ColumnIndex = 1 Then
            DirectCast(e.Control, TextBox).AutoCompleteMode = AutoCompleteMode.SuggestAppend
            DirectCast(e.Control, TextBox).AutoCompleteSource = AutoCompleteSource.CustomSource
            DirectCast(e.Control, TextBox).AutoCompleteCustomSource = RutasAutocomplete
        End If
       
        If dgPlan.CurrentCell.ColumnIndex > 1 Then
            DirectCast(e.Control, TextBox).AutoCompleteMode = Nothing
            'DirectCast(e.Control, TextBox).AutoCompleteSource = Nothing
            'DirectCast(e.Control, TextBox).AutoCompleteCustomSource = Nothing
        End If
    End Sub
    Sub CargaRutas()
        Rutas.Columns.Add("Nombre")

        Dim cnConn As New SqlConnection
        cnConn.ConnectionString = CitraConnection

        Dim strSql As String = ""
        strSql = "SELECT Nombre FROM Rutas ORDER BY Nombre"

        Dim cmdBuscar As New SqlCommand
        cmdBuscar.Connection = cnConn
        cmdBuscar.CommandText = strSql
        Dim rdBuscar As SqlDataReader
        cnConn.Open()
        rdBuscar = cmdBuscar.ExecuteReader
        Do While rdBuscar.Read()
            Rutas.Rows.Add(rdBuscar("Nombre").ToString.Trim)
        Loop
    End Sub
    Sub CargaPlan()
        dgPlan.Rows.Clear()

        Dim cnConn As New SqlConnection
        cnConn.ConnectionString = CitraConnection

        Dim TipoDia As String = lstDia.Text
        Dim intTipoDia As Integer = 0
        Select Case TipoDia
            Case "Hábil"
                intTipoDia = 1
            Case "Sábado"
                intTipoDia = 2
            Case "Domingo"
                intTipoDia = 3
            Case "Vacaciones"
                intTipoDia = 4
            Case "Especial"
                intTipoDia = 5
        End Select

        Dim strSql As String = ""
        strSql = "SELECT IdPlan,Nombre,Despachos,Vueltas,Autobuses FROM PlanOperacion P INNER JOIN Rutas R ON P.IdRuta=R.IdRuta AND TipoDia=" & intTipoDia & " ORDER BY IdPlan"


        Dim cmdBuscar As New SqlCommand
        cmdBuscar.Connection = cnConn
        cmdBuscar.CommandText = strSql
        Dim rdBuscar As SqlDataReader
        cnConn.Open()
        rdBuscar = cmdBuscar.ExecuteReader
        Do While rdBuscar.Read()
            dgPlan.Rows.Add(rdBuscar("IdPlan"), rdBuscar("Nombre"), rdBuscar("Despachos"), rdBuscar("Vueltas"), rdBuscar("Autobuses"))
        Loop

        dgPlan.Columns(2).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
        dgPlan.Columns(3).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
        dgPlan.Columns(4).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight

    End Sub

    Private Sub lstDia_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstDia.SelectedIndexChanged
        CargaPlan()

    End Sub

    Private Sub ToolStripButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton1.Click
        Close()

    End Sub

    Private Sub ToolStripButton4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton4.Click
        Dim IdPlan As Integer = 0
        Dim aux As String = dgPlan.CurrentRow.Cells(0).Value
        If Len(aux) > 0 Then
            IdPlan = dgPlan.CurrentRow.Cells(0).Value
            Call SetSqlCitra("DELETE FROM PlanOperacion WHERE IdPlan=" & IdPlan)
            CargaPlan()
        Else
            CargaPlan()
        End If

    End Sub
    Sub Guardar()
        Dim Plan As New clsPlanOperacion
        Dim Dia As String = lstDia.Text
        Dim aux As String = ""

        If Dia = "" Then
            MsgBox("Debes de indicar el tipo de dia", MsgBoxStyle.Exclamation, "Aviso")
            Exit Sub
        End If

        For i = 0 To dgPlan.Rows.Count - 1
            Plan.IdRuta = BuscarStrDatoCitra("SELECT IdRuta FROM Rutas WHERE Nombre='" & dgPlan.Rows(i).Cells(1).Value & "'")
            Select Case Dia
                Case "Hábil"
                    Plan.TipoDia = 1
                Case "Sábado"
                    Plan.TipoDia = 2
                Case "Domingo"
                    Plan.TipoDia = 3
                Case "Vacaciones"
                    Plan.TipoDia = 4
                Case "Especial"
                    Plan.TipoDia = 5
            End Select
            Plan.Despachos = dgPlan.Rows(i).Cells(2).Value
            Plan.Vueltas = dgPlan.Rows(i).Cells(3).Value
            Plan.Autobuses = dgPlan.Rows(i).Cells(4).Value
            Plan.Usuario = UsuarioActivo
            Plan.FechaUltimoCambio = Now()

            aux = dgPlan.Rows(i).Cells(0).Value
            If Len(aux) > 0 Then
                Plan.IdPlan = dgPlan.Rows(i).Cells(0).Value
                Plan.Actualizar()
            Else
                Plan.Insertar()
            End If
        Next



    End Sub

    Private Sub ToolStripButton5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton5.Click
        Try
            Guardar()
        Catch ex As Exception

        End Try
        MsgBox("Registros Guardados", MsgBoxStyle.Information, "Aviso")

    End Sub
End Class